package com.xhu.controller;

import com.xhu.base.ConnectionMySQL;
import com.xhu.base.MetaDataManager;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.update.Update;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

/**
 * 用户交互接口实现类
 * @author 王涛
 * @Created by WT on 2019/3/27.
 */
public class Client {
    /**
     * 这个变量用于收集语句中的DET类型的列名，用于处理RND层时使用
     * 因为我们只对DET类型的列增加RND层，而在一条语句改写后含有
     */
    public static List<String> tableNameList = new ArrayList<String>();
    public static List<String> encColumnNameList = new ArrayList<String>();
    public static Map<Integer, List> selectResultMap=new HashMap<>();
    public static  void ClientDo(String sql){
    PreProcessTest preTest = new PreProcessTest();
    System.out.println("当前默认sens是0.01，请注意数据精度,超出精度值会出现保序操作不准确");
    preTest.testSSDB();
    Connection conn = ConnectionMySQL.openConnection();
    String inputSQL = sql;

            try {
                CCJSqlParserManager parserManager = new CCJSqlParserManager();
                Statement statement = parserManager.parse(new StringReader(inputSQL));
                if (statement instanceof CreateTable) {
                    long start = System.currentTimeMillis();
                    CreateTable createTable = (CreateTable) statement;
                    CreateTableDeparser.handler(createTable,conn);
                    long end = System.currentTimeMillis();
                    System.out.println("CreateTime:"+(end-start));
                } else {
                    if (statement instanceof Insert) {
                        long startInsert = System.currentTimeMillis();
                        Insert insert = (Insert) statement;
                        InsertDeparser.handler(insert, conn);
                        long endInsert = System.currentTimeMillis();
                        System.out.println("InsertTime:"+(endInsert-startInsert));

                    } else {
                        if (statement instanceof Select) {
                            long startSelect = System.currentTimeMillis();
                            Select select = (Select) statement;
                            selectResultMap = SelectSQL.handler(select, conn);
                            long endSelect = System.currentTimeMillis();
                            System.out.println("SelectTime:"+(endSelect-startSelect));
                        } else {
                            if (statement instanceof Delete) {
                                long startDelete = System.currentTimeMillis();
                                Delete delete = (Delete) statement;
                                DeleteDeparser.handler(delete,conn);
                                long endDelete = System.currentTimeMillis();
                                System.out.println("DeleteTime:"+(endDelete-startDelete));
                            } else {
                                if (statement instanceof Update) {
                                    long startUpdate = System.currentTimeMillis();
                                    Update update = (Update) statement;
                                    UpdateDeparser.handler(update,conn);
                                    long endUpdate = System.currentTimeMillis();
                                    System.out.println("UpdateTime:"+(endUpdate-startUpdate));
                                } else {
                                    System.out.println("不支持的语句类型");
                                    return;
                                }
                            }
                        }
                    }
                }
                //在执行完一条语句后，需要将静态列表清空
                tableNameList.clear();
                encColumnNameList.clear();
  /*              System.out.println("是否继续使用?如需继续使用请直接输入下一条语句,否则输入exit：");
                Scanner endSc = new Scanner(System.in);
                String endStr= endSc.nextLine();
                if(endStr.contains("exit")){
                    endSc.close();
                    break;
                }else{
                    inputSQL = endStr;
                    continue;
                }*/

            } catch (JSQLParserException e) {
                e.printStackTrace();
            }
        }



    public static void main(String[] args) throws InterruptedException {
        //ClientDo("create table user(id int,name varchar(10),password varchar(10))");
        //ClientDo("insert into user(1,'admin','adminadmin')");
        MetaDataManager metaDataManager = new MetaDataManager();
        List allTableName = metaDataManager.getAllTableName();
        System.out.println( "");
    }

     	/*
 	 * 创建表
 	 * create table stu(id int,name varchar(20),age int,sex char(5));
 	 * create table gra(id int,gra double);
 	 * create table user(id int,name varchar(10),password varchar(10));
 	 *
 	 *
 	 *insert into user(id,name,password) values(1,'admin','adminadmin');
 	 * 插入
 	 * insert into stu(id,name,age,sex) values(1,'chandler',20,'male');
 	 * insert into stu(id,name,age,sex) values(2,'joe',22,'male');
 	 * insert into stu(id,name,age,sex) values(3,'monica',19,'female');
 	 * insert into stu(id,name,age,sex) values(4,'ross',20,'male');
 	 * insert into stu(id,name,age,sex) values(5,'phebe',20,'female');
 	 * insert into stu(id,name,age,sex) values(6,'rachel',20,'female');
 	 *
 	 * insert into gra(id,gra) values(1,95);
 	 * insert into gra(id,gra) values(2,86);
 	 * insert into gra(id,gra) values(3,72);
 	 * insert into gra(id,gra) values(4,97);
 	 * insert into gra(id,gra) values(5,95);
 	 * insert into gra(id,gra) values(6,70);
 	 *
 	 * 等值查询DET
 	 * select id,name,age,sex from stu;
 	 * select id from stu where name = 'chandler';
 	 *
 	 * 范围查询OPE
 	 * select name from stu where age >= 20;
 	 *
 	 * select id,gra from gra;
 	 * select id from gra where gra > 90;
 	 * select avg(gra) from gra;
 	 *
 	 * select stu.id from stu inner join gra on stu.id = gra.id where gra.gra >= 80;
 	 * 等值连接JOIN
 	 *
 	 * select gra.gra from stu inner join gra on stu.id = gra.id where stu.name = 'chandler';
 	 *
 	 * 等值连接+加法同态
 	 * select avg(gra.gra) from gra;
 	 * select avg(gra.gra) from stu inner join gra on stu.id = gra.id where gra.gra > 80;
 	 *
 	 * 更新
 	 * update gra set gra = 100 where id = 1;
 	 *
 	 * 带条件的删除操作
 	 * delete from gra where gra < 80;
 	 * delete from gra where gra > 80;
 	 */
}
